package org.jeecg.modules.mall.job;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.show.api.ShowApiRequest;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.mall.entity.Pagebean;
import org.jeecg.modules.mall.entity.TbNewsContent;
import org.jeecg.modules.mall.entity.Tb_news2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class NewsUtils {

    public static String needContent = "1";
    public static String needAllList = "1";

    public static void saveNewsBeanData(String channelId, String name, String page, String maxResult) {
        String res = new ShowApiRequest("http://route.showapi.com/109-35", "198037", "a6503ede25454bfdbab11b91689abb17")
                .addTextPara("channelId", channelId)
                .addTextPara("channelName", name)
                //.addTextPara("title", "足球")
                .addTextPara("needContent", needContent)
                .addTextPara("needHtml", "0")
                .addTextPara("needAllList", needAllList)
                .addTextPara("page", page)
                .addTextPara("maxResult", maxResult)
                .addTextPara("id", "")
                .post();
        JSONObject jsonObject = JSON.parseObject(res);
        Integer res_code = jsonObject.getInteger("showapi_res_code");
        if (res_code == 0) {
            String contentlist = jsonObject.getString("showapi_res_body");
            JSONObject resBodyjsonObject = JSON.parseObject(contentlist);
            String pagebean = resBodyjsonObject.getString("pagebean");
            Pagebean pagebeanData = JSON.parseObject(pagebean, new TypeReference<Pagebean>() {
            });

            Integer allpage = pagebeanData.getAllPages();
            Integer currentPage = pagebeanData.getCurrentPage();
            List<TbNewsContent> tbNewsContents = pagebeanData.getContentlist();
            if (null != tbNewsContents && tbNewsContents.size() > 0) {
                for (TbNewsContent tbNewsContent : tbNewsContents) {
                 //   TbNewsContent tbNewsContent1 = tbNewsContentService.getById(tbNewsContent.getId());
                 //   if (null != tbNewsContent1) {
                 //       tbNewsContents.remove(tbNewsContent);
                 //   }
                }
              //  tbNewsContentService.saveBatch(tbNewsContents);
            }
            if (currentPage != allpage) {
                Integer new_page = currentPage + 1;
                saveNewsBeanData(channelId, name, String.valueOf(new_page), maxResult);
            }

        }
    }

    public static List<Tb_news2> getNewsData(){

        String url = "http://v.juhe.cn/toutiao/index";
        Map<String, Object> sortParams = new HashMap<>();
        sortParams.put("type", "top");
        sortParams.put("key", "9b89e4b54861d1e2da1633565a71a473");
        String response = HttpUtil.get(url,sortParams);
        JSONObject jsonObject = JSON.parseObject(response);
        String jsondata=JSON.parseObject(jsonObject.getString("result")).getString("data");
        List<Tb_news2> pagebeanDataList = JSON.parseArray(jsondata,Tb_news2.class);
        return pagebeanDataList;
    }
    public static void main(String[] args) {

     //   saveNewsBeanData("9a15d3d4b5cab17f34e6d7976b1b1f30","电商最新","1","10");
        getNewsData();
    }


}
